<template>
    <div class="detailFoot">
        <input type="text" ref="input" v-model="value" @keydown.enter="send"/>
        <button @click="send">发送</button>
    </div>
</template>

<script>
import axios from 'axios'
import jwtDecode from 'jwt-decode'
import { Toast } from 'vant'
export default { 
    name:'CommentButton',
    props:['bodyId','postUserId','bodyMessage','isPost'],
    data() {
        return {
            value:'',
            decode:null,
            login:null//判断有没有登录
        }
    },
    mounted() {
        if(sessionStorage.getItem('token')){
            this.$refs.input.focus()
            let token=sessionStorage.getItem("token")
            this.decode = jwtDecode(token);
            this.login = true
        }else{
            this.login = false
        }
    },
    methods: {
        send(){
            if(this.login){
                let that = this
                // 判断是不是评论的帖子
                if(that.isPost){
                    //评论
                    let sendParam = new URLSearchParams()
                    sendParam.append('bodyId',String(this.bodyId))
                    sendParam.append('context',this.value)
                    sendParam.append('userId',String(this.decode.userId))
                    axios({
                        url:"/praise/addComment",
                        method:'post',
                        data:sendParam,
                        headers:{
                            'Authentication-Token':JSON.parse(sessionStorage.getItem("token"))
                        }
                    }).then(data=>{
                        if(data.data.code === 20000){
                            console.log('评论成功')
                            //给服务器发送消息
                            let sendParam = new URLSearchParams()
                            sendParam.append('bodyMessage',that.bodyMessage)
                            sendParam.append('content',`${that.decode.nickname} 评论了你的帖子`)
                            sendParam.append('form',String(that.decode.userId))
                            sendParam.append('to',String(this.postUserId))
                            sendParam.append('username',that.decode.username)
                            sendParam.append('classes',String(1))
                            axios({
                                method:'post',
                                url:'/body/sendMessage',
                                data:sendParam,
                                headers:{
                                    'Authentication-Token':JSON.parse(sessionStorage.getItem("token"))
                                }
                            }).then(data=>{
                                if(data.data.code === 20000){
                                    console.log('成功发送')
                                    //查看当前帖子下的所有评论
                                    axios({
                                        url:'/praise/findComment',
                                        method:'get',
                                        params:{
                                            bodyId:this.bodyId,
                                        },
                                        headers:{ 
                                            'Authentication-Token':JSON.parse(sessionStorage.getItem("token"))
                                        }
                                    }).then(data=>{
                                        let res = data.data.data
                                        //触发自定义事件
                                        this.$emit('changeComments',res)
                                        console.log(res)
                                    }).catch(err=>{
                                        console.log(err)
                                    })
                                }
                            }).catch(err=>{
                                console.log(err)
                            })
                        }
                        this.value = ''
                    }).catch(err=>{
                        console.log(err)
                    })
                    
                }else{
                    //回复帖子
                    let sendParam = new URLSearchParams()
                    sendParam.append('commentId',String(that.bodyId))
                    sendParam.append('context',this.value)
                    sendParam.append('userId',String(this.decode.userId))
                    axios({
                        url:"/praise/addSonComment",
                        method:'post',
                        data:sendParam,
                        headers:{
                            'Authentication-Token':JSON.parse(sessionStorage.getItem("token"))
                        }

                    }).then(data=>{
                        if(data.data.code === 20000){
                            console.log('评论成功')
                            //给服务器发送消息
                            let sendParam = new URLSearchParams()
                            sendParam.append('bodyMessage',that.bodyMessage)
                            sendParam.append('content',`${that.decode.nickname} 回复了你的评论`)
                            sendParam.append('form',String(that.decode.userId))
                            sendParam.append('to',String(this.postUserId))
                            sendParam.append('username',that.decode.username)
                            sendParam.append('classes',String(1))
                            axios({
                                method:'post',
                                url:'/body/sendMessage',
                                data:sendParam,
                                headers:{
                                    'Authentication-Token':JSON.parse(sessionStorage.getItem("token"))
                                }
                            }).then(data=>{
                                if(data.data.code === 20000){
                                    console.log('成功发送')
                                }
                            }).catch(err=>{
                                console.log(err)
                            })
                        }
                        this.value = ''
                    }).catch(err=>{
                        console.log(err)
                    })
                    //查看当前评论下的所有子评论
                    axios({
                        url:'/praise/findSonComment',
                        method:'get',
                        params:{
                            commentId:this.$route.params.id,
                            page:this.pageNum
                        },
                        headers:{
                            'Authentication-Token':JSON.parse(sessionStorage.getItem("token"))
                        }
                    }).then(data=>{
                        let res = data.data.data
                        //触发自定义事件
                        this.$emit('changeComments',res)
                    }).catch(err=>{
                        console.log(err)
                    })
                }
                
                //这里可能有点问题得用async写
            }else{
                Toast('请先登录或注册')
            }
        }
    },
}
</script>

<style lang="less" scoped>
    .detailFoot{
        // box-shadow: 1rem 0 0.1rem #bdbdbd;
        padding: 0.2rem 0.3rem;
        display: flex;
        align-items: center;
        border-top: solid 1px #f0f0f0;
        input{
            flex: 1;
            padding: 0.1rem 0.3rem;
            outline: none;
            border: none;
            font-size: 0.4rem;
            text-indent: 0.15rem;
            background-color: #f3f3f3;
            margin-right: 0.1rem;

        }
        button{
            background-color: var(--themeColor);
            outline: none;
            border: none;
            color: white;
            padding: 0.1rem 0.3rem;
        }
    }
</style>